//获取应用实例
const app = getApp();
const db = wx.cloud.database();
import {
  Router
} from "../../utils/common.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_url: '', //图片地址

    // 卡片模块：
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //切换栏的滚动条位置
    cardImgSrc: '', //存储卡片图
    //卡片数组
    cardArr: [{
        id: '01',
        img: 'cloud://pets-7guwp737b4dd6219.7065-pets-7guwp737b4dd6219-1304125172/bgCard/bg1.jpg'
      },
      {
        id: '02',
        img: 'cloud://pets-7guwp737b4dd6219.7065-pets-7guwp737b4dd6219-1304125172/bgCard/bg2.jpg'
      },
      {
        id: '03',
        img: 'cloud://pets-7guwp737b4dd6219.7065-pets-7guwp737b4dd6219-1304125172/bgCard/bg3.jpg'
      },
      {
        id: '04',
        img: 'cloud://pets-7guwp737b4dd6219.7065-pets-7guwp737b4dd6219-1304125172/bgCard/bg4.jpg'
      },
      {
        id: '05',
        img: 'cloud://pets-7guwp737b4dd6219.7065-pets-7guwp737b4dd6219-1304125172/bgCard/bg5.jpg'
      },
      {
        id: '06',
        img: 'cloud://pets-7guwp737b4dd6219.7065-pets-7guwp737b4dd6219-1304125172/bgCard/bg6.jpg'
      },
    ],
    petWords: "",
    petName: "",
    openid: ""
  },

  // 点击切换当前页时改变样式
  swichNav: function (e) {
    var that = this,
      cur = e.target.dataset.current,
      src = e.target.dataset.src;
    if (that.data.currentTaB == cur) {
      return false;
    } else {
      that.setData({
        currentTab: cur
      })
    }
    that.setData({
      cardImgSrc: src
    })
    that.checkCor();
  },
  //判断当前滚动超过一屏时
  checkCor: function (e) {
    var that = this;
    if (that.data.currentTab > 2) {
      that.setData({
        scrollLeft: 300
      })
    } else {
      that.setData({
        scrollLeft: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let pic = "/style/images/download.png";
    let base64 = 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync(pic, 'base64');
    let openid = wx.getStorageSync('openid');
    this.setData({
      openid: openid,
      img_url: base64,
      cardImgSrc: this.data.cardArr[0].img
    })
    // 随机获取宠物信息
    this.loadPetDet();
    // 获取getPetWords
    this.getPetWords();

  },

  /**
   * 获取宠物信息
   */
  loadPetDet() {
    db.collection('p_pets').where({
      _openid: this.data.openid
    }).limit(1).get({
      success: res => {
        let info = res.data[0];
        this.setData({
          petName: info.petName
        });
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  /**
   * 获取petWords
   */
  getPetWords() {
    db.collection('p_pet_card_words').where({
        id: this.sum(27, 52)
      }).limit(1)
      .get({
        success: res => {
          this.setData({
            petWords: res.data[0].cardWords
          })
        },
        fail: err => {
          console.log('[数据库] [查询记录] 失败：');
        }
      })
  },

  /**
   * 获取随机数工具
   */
  sum(m, n) {
    var num = Math.floor(Math.random() * (m - n) + n);
    return num;
  },

  postCard() {
    let cardImgSrc = this.data.cardImgSrc;
    wx.cloud.downloadFile({
      fileID: cardImgSrc,
      success: result => {
        cardImgSrc = result.tempFilePath;
        console.log(cardImgSrc);
        let params = {
          petName: this.data.petWords + " " + this.data.petName,
          cardBg: cardImgSrc
        }
        Router.navigateTo("../greeting-card/greeting-card", params);

      },
      fail: err => {
        console.log('读取图片信息失败')
      }
    })
  }


})